package com.airlines.crewassignment.controller.rule;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.user.User;
import com.airlines.common.ListSortUtil;
import com.airlines.common.R;
import com.airlines.crewassignment.domain.rule.AssignmentRule;
import com.airlines.crewassignment.service.rule.AssignmentRuleService;

/**
 * @author xxxx
 * @date 2018/05/16 09:30:29
 * @company xxxx Airlines
 * @description 
 */
@Controller
@RequestMapping("assignmentRule")
public class AssignmentRuleController {
	public static final String PREFIX = "/rule";
    

    @Autowired
    AssignmentRuleService assignmentRuleService;
    /**
     * 进入页面
     * http://172.16.12.159:8080/assignmentRule/rule
     * @return
     */
    @RequestMapping(value = "rule", method={ RequestMethod.GET})
    //@RequiresPermissions("CAS:assignmentRule:list")
    String toCrewAssigmengtRulePage(){
        return PREFIX + "/assignmentRuleList";
    }

    /**
     * 多条件模糊 查询
     * @param assignmentRule
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
   // @RequiresPermissions("CAS:assignmentRule:list")
    public List<AssignmentRule> list(AssignmentRule assignmentRule) {
    	List<AssignmentRule> list =  assignmentRuleService.listByFuzzy(assignmentRule);
    	ListSortUtil.sort(list, true, "ruleCode");
        return list;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add", method={ RequestMethod.GET})
    //@RequiresPermissions("CAS:assignmentRule:add")
    String add(){
        return PREFIX + "/assignmentRuleAdd";
    }

    /**
     * 保存
     * @param crewAssigmengtRule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method={ RequestMethod.POST})
   // @RequiresPermissions("CAS:assignmentRule:add")
    public R save(AssignmentRule assignmentRule,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        assignmentRule.setUpdateBy(user.getWorkcode());
        if(assignmentRuleService.insert(assignmentRule) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入编辑页面
     * @param model
     * @param ruleId
     * @return
     */
    @RequestMapping(value = "/edit", method={ RequestMethod.GET})
   // @RequiresPermissions("CAS:assignmentRule:edit")
    String edit(Model model,Integer ruleId){
    	AssignmentRule assignmentRule = assignmentRuleService.get(ruleId);
        model.addAttribute("assignmentRule", assignmentRule);
        return PREFIX + "/assignmentRuleEdit";
    }

    /**
     * 修改
     * @param crewAssigmengtRule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method={ RequestMethod.POST})
  //  @RequiresPermissions("CAS:assignmentRule:edit")
    public R update(AssignmentRule assignmentRule,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        assignmentRule.setUpdateBy(user.getWorkcode());
        if(assignmentRuleService.update(assignmentRule)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入查看页面
     * @param model
     * @param ruleId
     * @return
     */
    @RequestMapping(value = "/view", method={ RequestMethod.GET})
   // @RequiresPermissions("CAS:assignmentRule:list")
    String view(Model model,Integer ruleId){
    	AssignmentRule assignmentRule = assignmentRuleService.get(ruleId);
        model.addAttribute("assignmentRule", assignmentRule);
        model.addAttribute("isReadonly", 1);
        return PREFIX + "/assignmentRuleEdit";
    }

    /**
     * 删除
     * @param ruleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method={ RequestMethod.POST})
   // @RequiresPermissions("CAS:assignmentRule:delete")
    public R delete( Integer ruleId){
        if(assignmentRuleService.deleteLogic(ruleId)>0){
            return R.ok();
        }
        return R.error();
    }
    
    
    /**
     * 删除
     * @param ruleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/disable", method={ RequestMethod.POST})
    public R disable( Integer ruleId){
        if(assignmentRuleService.disable(ruleId)>0){
            return R.ok();
        }
        return R.error();
    }
    
    @ResponseBody
    @RequestMapping(value = "/able", method={ RequestMethod.POST})
    public R able( Integer ruleId){
        if(assignmentRuleService.able(ruleId)>0){
            return R.ok();
        }
        return R.error();
    }

}
